Skip to content

Slight modification to the as_ref example of std::option::Option #48509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 6, 2018

Conversation

Phlosioneer
Copy link
Contributor

A user in a reddit thread was confused by the name of the variable
"num_as_int"; they thought the example was trying to convert the
string "10" as if it were binary 2 by calling str::len(). In reality,
the example is simply demonstrating how to take an immutable reference
to the value of an Option. The confusion comes from the coincidence
that the length of the string "10" is also its binary representation,
and the implication from the variable names that a conversion was
occuring ("num_as_str" to "num_as_int").

This PR changes the example number to 12 instead of 10, and changes
the variable name from "num_as_int" to "num_length" to better
communicate what the example is doing.

The reddit thread:
https://www.reddit.com/r/rust/comments/7zpvev/notyetawesome_rust_what_use_cases_would_you_like/dur39xw/

A user in a reddit thread was confused by the name of the variable
"num_as_int"; they thought the example was trying to convert the
string "10" as if it were binary 2 by calling str::len(). In reality,
the example is simply demonstrating how to take an immutable reference
to the value of an Option. The confusion comes from the coincidence
that the length of the string "10" is also its binary representation,
and the implication from the variable names that a conversion was
occuring ("num_as_str" to "num_as_int").

This PR changes the example number to 12 instead of 10, and changes
the variable name from "num_as_int" to "num_length" to better
communicate what the example is doing.

The reddit thread:
https://www.reddit.com/r/rust/comments/7zpvev/notyetawesome_rust_what_use_cases_would_you_like/dur39xw/
@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @TimNN (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 24, 2018
@@ -233,10 +233,10 @@ impl<T> Option<T> {
/// [`usize`]: ../../std/primitive.usize.html
///
/// ```
/// let num_as_str: Option<String> = Some("10".to_string());
/// let num_as_str: Option<String> = Some("12".to_string());
Copy link

@neandrake neandrake Feb 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To address the problem more directly it might make sense to change this completely -- the fact that the string contents are numbers is irrelevant. Maybe instead

let text: Option<String> = Some("Hello, World!".to_string());
let text_length: Option<usize> = text.as_ref().map(|t| t.len());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea. I'll change that.

The choice of string is arbitrary, so all references to a number
in the string were removed. The string is now the standard "Hello
world!".
Tried to be fancy with print statements.
@Phlosioneer
Copy link
Contributor Author

@neandrake I fixed what you commented on, could you review this again when you get a chance?

Copy link

@neandrake neandrake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea it looks good to me. I'm not sure if I can approve though I'm not part of the project.

@Phlosioneer
Copy link
Contributor Author

Who needs to review this, then? We should probably r? someone who is able to merge it.

@Phlosioneer
Copy link
Contributor Author

@TimNN Could you review this / merge it? Or point me to someone who can?

@TimNN
Copy link
Contributor

TimNN commented Mar 6, 2018

@bors r+ rollup.

Thanks for the PR, @Phlosioneer! Sorry for the delay in reviewing.

@bors
Copy link
Collaborator

bors commented Mar 6, 2018

📌 Commit b7b3498 has been approved by TimNN

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 6, 2018
@bors
Copy link
Collaborator

bors commented Mar 6, 2018

⌛ Testing commit b7b3498 with merge 6f2100b...

bors added a commit that referenced this pull request Mar 6, 2018
Slight modification to the as_ref example of std::option::Option

A user in a reddit thread was confused by the name of the variable
"num_as_int"; they thought the example was trying to convert the
string "10" as if it were binary 2 by calling str::len(). In reality,
the example is simply demonstrating how to take an immutable reference
to the value of an Option. The confusion comes from the coincidence
that the length of the string "10" is also its binary representation,
and the implication from the variable names that a conversion was
occuring ("num_as_str" to "num_as_int").

This PR changes the example number to 12 instead of 10, and changes
the variable name from "num_as_int" to "num_length" to better
communicate what the example is doing.

The reddit thread:
https://www.reddit.com/r/rust/comments/7zpvev/notyetawesome_rust_what_use_cases_would_you_like/dur39xw/
@bors
Copy link
Collaborator

bors commented Mar 6, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: TimNN
Pushing 6f2100b to master...

@bors bors merged commit b7b3498 into rust-lang:master Mar 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants